﻿/// <summary>
/// Player character.cs
/// Gibbie Chairul
/// 3/29/2015
/// 
/// This script controls the users ingame characture. Make sure this script is attached to the character prefab
/// </summary>

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[AddComponentMenu("Player/Player Character Stats")]
//Inherit from base character
public class PlayerCharacter : BaseCharacter {
//	public GameObject playerPreb;
	public const string PLAYER_TAG = "Player";
//	private Transform displayName;

	static public GameObject nameTarget;
	private List<Item> inventory = new List<Item>();
	public List<Item> Inventory{
		get{ return inventory;}
		set{ inventory = value;}
	}

//	public Item _equipedWeapon;

	public bool initialized = false;

	private static PlayerCharacter instance = null;
	public static PlayerCharacter Instance{
		get{
			if(instance == null){
//				Debug.Log("instanceing a new pc");

				GameObject go = Instantiate( Resources.Load("Character/SirBearington_Prefab")) as GameObject;
			

				PlayerCharacter temp = go.GetComponent<PlayerCharacter>();;

				if(temp == null)
					Debug.LogError("Player Prefab does not contain an PC Script. Please add an configure");

				instance = go.GetComponent<PlayerCharacter>();
				go.name = "PC";
				go.tag = "Player";
			}
			return instance;

		}
	}
	public void Initialize(){


		if (!initialized)
			LoadCharacter ();
	}
	#region unity function
	public new void Awake()
	{
		base.Awake();
		//Debug.Log(Resources.Load("Character/Model"));

		EquipedWeapon = ItemGenerator.CreateItem( ItemType.MeleeWeapon);
		EquipShield  = ItemGenerator.CreateItem( ItemType.MeleeWeapon);
		instance = this;
	}
	//we do not want to be sinding messages out each frame. We will be moving this out when we get back in to combat
	void Update(){
		//Make the mob names following target(Player)
//		displayName.LookAt(nameTarget.transform);
		//Debug.Log (target.transform);
//		displayName.Rotate(new Vector3(0,180,0));
		//Messenger<int,int>.Broadcast("player health update", 90, 100,MessengerMode.DONT_REQUIRE_LISTENER);
//		if (GetVital (0).CurValue <= 0) {
//			Destroy(gameObject);
//			Application.LoadLevel("Menu");
//			GetVital(0).CurValue = GetVital(0).AdjustedBaseValue ;
//		}
	}
	#endregion
	



	public Item EquipedShield{
		get{ return _equipment[(int)EquipmentSlot.OffHand]; }
		set{ 
			_equipment[(int)EquipmentSlot.OffHand] = value;
			
			if( offHandMount.transform.childCount > 0)
				Destroy(offHandMount.transform.GetChild(0).gameObject);

			if(_equipment[(int)EquipmentSlot.OffHand] != null){
				Debug.Log(GameSetting.SHIELD_ICON_PATH + _equipment[(int)EquipmentSlot.OffHand].Name);
				GameObject mesh = Instantiate(Resources.Load(GameSetting.SHIELD_MESH_PATH + _equipment[(int)EquipmentSlot.OffHand].Name),offHandMount.transform.position,offHandMount.transform.rotation)as GameObject;
				mesh.transform.parent = offHandMount.transform ;
			}
			
		}
	}
	public Item EquipedHead{
		get{ return _equipment[(int)EquipmentSlot.Head]; }
		set{ 
			_equipment[(int)EquipmentSlot.Head] = value;
			
			if( headMount.transform.childCount > 0)
				Destroy(headMount.transform.GetChild(0).gameObject);
			
			if(_equipment[(int)EquipmentSlot.Head] != null){
				Debug.Log(GameSetting.SHIELD_ICON_PATH + _equipment[(int)EquipmentSlot.Head].Name);
				GameObject mesh = Instantiate(Resources.Load(GameSetting.HEAD_MESH_PATH + _equipment[(int)EquipmentSlot.Head].Name),headMount.transform.position,headMount.transform.rotation)as GameObject;
				mesh.transform.parent = headMount.transform ;
			}
			
		}
	}

	public Item EquipedTorso{
		get{ return _equipment[(int)EquipmentSlot.Torso]; }
		set{ 
			_equipment[(int)EquipmentSlot.Torso] = value;
			
			if( torsoMount.transform.childCount > 0)
				Destroy(torsoMount.transform.GetChild(0).gameObject);
			
			if(_equipment[(int)EquipmentSlot.Torso] != null){
				Debug.Log(GameSetting.HEAD_MESH_PATH + _equipment[(int)EquipmentSlot.Torso].Name);
				GameObject mesh = Instantiate(Resources.Load(GameSetting.TORSO_MESH_PATH + _equipment[(int)EquipmentSlot.Torso].Name),torsoMount.transform.position,torsoMount.transform.rotation)as GameObject;
				mesh.transform.parent = torsoMount.transform ;
			}
			
		}
	}

	public void LoadCharacter(){
		nameTarget = GameObject.FindGameObjectWithTag("MainCamera"); 
		GameSetting.LoadAttributes ();
		ClearModifers ();
		GameSetting.LoadVitals ();
		GameSetting.LoadSkills ();
		Messenger<int,int>.Broadcast("player health update", GetVital(0).CurValue, GetVital(0).AdjustedBaseValue,MessengerMode.REQUIRE_LISTENER);
		//find the cname component
	//	displayName = transform.FindChild("Name");
		//change the display name to what we want
		//displayName.GetComponent<TextMesh>().text = GameSetting.LoadName();
		initialized = true;

	}
	public void LoadArmor(){
	}
}

